package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.OrderService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.DES3Util;
import com.edais.util.JsonUtil;
import com.edais.util.LoginRedirectUtil;
import com.edais.util.Message;
import com.edais.util.weixinAbout.HttpRequestParam;

/** 订单action */
@Controller
@RequestMapping("/order")

public class OrderAction implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2113374375848529723L;

	/** 日志 */
	private static final Log logger = LogFactory.getLog(OrderAction.class);

	/** 设置service */
	@Autowired
	private OrderService orderService;
	
	
	/**
	 * 盖章按钮
	 * @date 2016-12-22
	 * @author gongyl
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/doSeal")
	public void doSeal(HttpServletRequest request, HttpServletResponse response) {
		try {
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String sealCode = paramsMap.get("sealCode");
		String sealPwd = paramsMap.get("sealPwd");
		String agreementNo = paramsMap.get("agreementNo");
		String sealBeginTime = paramsMap.get("sealBeginTime");
		String sealEndTime = paramsMap.get("sealEndTime");
		String keyWord = paramsMap.get("keyWord");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("sealCode", sealCode);
		map.put("sealPwd", DES3Util.encode(String.valueOf(sealPwd)));
		map.put("sealBeginTime", sealBeginTime);
		map.put("sealEndTime", sealEndTime);
		map.put("agreementNo", agreementNo);
		map.put("keyWord", keyWord);
		orderService.doSeal(map);
		String result="00000";
		CommonUtil.responseJson(result, response);// 处理返回结果
		} catch (Exception e) {
			logger.error("Order/getOrderInfoList 盖章出现异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	/**
	 * 印章订单列表管理
	 * @date 2016-12-22
	 * @author gongyl
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOrderList4Seal")
	public void getOrderList4Seal(HttpServletRequest request, HttpServletResponse response) {
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String currentPageStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);// 当前页
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);// 页大小
		Integer current = 1;
		Integer pageSize = 10;// 默认显示10条
		try {
			// 分页参数
			if (currentPageStr != null && !("".equals(currentPageStr))) {
				current = Integer.parseInt(currentPageStr);
			}
			if (pageSizeStr != null && !("".equals(pageSizeStr))) {
				pageSize = Integer.parseInt(pageSizeStr);
			}
			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("current", current);
			map.put("pageSize", pageSize);
			map.put("startEndDate",paramsMap.get("startEndDate"));
			map.put("endEndDate",paramsMap.get("endEndDate"));
			String result = orderService.getOrderList4Seal(map);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("Order/getOrderInfoList 异常:", e);
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	/**
	 * 功能描述：查询章编码列表
	 * @date 2016-12-22
	 * @author gongyl
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSealCodeList")
	public void getSealCodeList(HttpServletRequest request, HttpServletResponse response) {
		try {
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		Map<String,Object> map=new HashMap<String,Object>(paramsMap);
		String param = CommonUtil.getParam(map);
		try {
			param = DES3Util.encode(param);
		} catch (Exception e) {
			logger.error("加密失败:",e);
			throw e;
		}
			// 调用javaservice_base接口
		String resultMsg2 = HttpRequestParam.sendGet(LoginRedirectUtil.quanhougamePath + "/seal/getSealCodeList",param);
					resultMsg2 = java.net.URLDecoder.decode(DES3Util.decode(resultMsg2), "UTF-8");
					logger.info("/order/getSealCodeList方法返回结果:" + resultMsg2);
			// 处理返回结果
			CommonUtil.responseJson(resultMsg2, response);
		} catch (Exception e) {
			logger.error("Order/getSealCodeList异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	

	/**
	 * 保存PDF文件路径到订单信息表中
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/savePDFPathOfOrder")
	public void savePDFPathOfOrder(HttpServletRequest request, HttpServletResponse response) {
		try {
			// 解密请求参数
			Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
			// 必须参数未传入
			if (StringUtils.isBlank(paramsMap.get("reqParams"))) {
				logger.error("保存PDF文件路径失败:请求参数为空！");
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE,
								Message.CnDescriptionExpression.PARAMS_IS_NULL, "");
				CommonUtil.responseJson(result, response);
				return;
			}
			
			Map<String,Object> map=new HashMap<String,Object>(paramsMap);
			String result=orderService.savePDFPathOfOrder(map);
			CommonUtil.responseJson(result, response);// 处理返回结果
			
		} catch (Exception e) {
			logger.error("order/savePDFPathOfOrder 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, Message.CnDescriptionExpression.BUSINESSE_ERROR, "");
			CommonUtil.responseJson(result, response);// 处理返回结果
			return;
		}
	}
	/**
	 * 获取PDF文件路径
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/getPDFPathByOrderId")
	public void getPDFPathByOrderId(HttpServletRequest request, HttpServletResponse response) {
		try {
			// 解密请求参数
			Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
			//必须参数未传入
			if (StringUtils.isBlank(paramsMap.get("userId"))) {
				logger.error("获取PDF文件路径失败:userId为空！");
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE,Message.CnDescriptionExpression.PARAMS_IS_NULL, "");
				CommonUtil.responseJson(result, response);
				return;
			}
			if (StringUtils.isBlank(paramsMap.get("orderId"))) {
				logger.error("获取PDF文件路径失败:orderId为空！");
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE,Message.CnDescriptionExpression.PARAMS_IS_NULL, "");
				CommonUtil.responseJson(result, response);
				return;
			}
			Map<String,Object> map=new HashMap<String,Object>(paramsMap);
			String result=orderService.getPDFPathByOrderId(map);
			CommonUtil.responseJson(result, response);// 处理返回结果
		} catch (Exception e) {
			logger.error("order/getPDFPathByOrderId 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, Message.CnDescriptionExpression.BUSINESSE_ERROR, "");
			CommonUtil.responseJson(result, response);// 处理返回结果
			return;
		}
	}
	
	
	/**
	 * 投资列表
	 * 
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOrderInfoList")
	public void getOrderInfoList(HttpServletRequest request, HttpServletResponse response) {

		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String type = paramsMap.get("type");// 1、处理中，2、还款中，3、已完成
		String userId = paramsMap.get("userId");
		String currentPageStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);// 当前页
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);// 页大小
		Integer current = 1;
		Integer pageSize = 10;// 默认显示10条

		// 程序必须参数未传入
		if (StringUtils.isBlank(userId)) {
			logger.error("获取投资列表信息数据检查失败:用户id为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}

		try {
			// 分页参数
			if (currentPageStr != null && !("".equals(currentPageStr))) {
				current = Integer.parseInt(currentPageStr);
			}
			if (pageSizeStr != null && !("".equals(pageSizeStr))) {
				pageSize = Integer.parseInt(pageSizeStr);
			}

			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("currentPage", current);
			map.put("pageSize", pageSize);
			map.put("type", type);
			map.put("userId", userId);

			String result = orderService.getOrdeInfoList(map);

			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("Order/getOrderInfoList 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}

	/**
	 * 
	 * @Description: 根据订单id查询订单详情
	 * @param request
	 * @param response
	 * @return void
	 * @throws @author
	 *             qibo
	 * @date 2015-9-4 下午3:26:30
	 */
	@RequestMapping(value = "/getOrderFormDetails")
	public void getOrderFormDetails(HttpServletRequest request, HttpServletResponse response) {
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		// 订单id
		String orderId = paramsMap.get("orderId");
		// 标的id
		String loanId = paramsMap.get("loanId");
		// 标的id
		String userId = paramsMap.get("userId");
		// 程序必须参数未传入
		if (StringUtils.isBlank(orderId) && StringUtils.isBlank(loanId)) {
			logger.error("获取投资列表信息数据检查失败:订单id或标的id为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}

		try {

			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderId", orderId);
			map.put("loanId", loanId);
			map.put("userId", userId);
			map.put("couponsRateRises", paramsMap.get("couponsRateRises"));

			String result = orderService.getOrderFormDetails(map);

			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("Order/getOrderFormDetails 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}

	/**
	 * 
	 * @Description: 根据订单id查询借款人详情
	 * @param request
	 * @param response
	 * @return void
	 * @throws @author
	 *             qibo
	 * @date 2015-9-4 下午5:21:41
	 */
	@RequestMapping(value = "/getBorrowerDetail")
	public void getBorrowerDetail(HttpServletRequest request, HttpServletResponse response) {
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		// 订单id
		String orderId = paramsMap.get("orderId");
		// 程序必须参数未传入
		if (StringUtils.isBlank(orderId)) {
			logger.error("获取投资列表信息数据检查失败:订单id为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}

		try {

			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderId", orderId);

			String result = orderService.getOrderFormDetails(map);

			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("Order/getOrderFormDetails 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}

	/**
	 * 债权详情
	 * 
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCreditInfoList")
	public void getCreditInfoList(HttpServletRequest request, HttpServletResponse response) {

		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String orderId = paramsMap.get("orderId");// 订单

		if (StringUtils.isBlank(orderId)) {
			logger.error("获取债权详情信息数据检查失败:订单id为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}

		try {
			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderId", orderId);

			String result = orderService.getCreditInfoList(map);

			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("Order/getCreditInfoList 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}

	/**
	 * 债权转让说明
	 * 
	 * @param request
	 * @return response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getAssignApplyExplain")
	public void getAssignApplyExplain(HttpServletRequest request, HttpServletResponse response) {

		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String orderId = paramsMap.get("orderId");
		String userId = paramsMap.get("userId");

		if (StringUtils.isBlank(orderId)) {
			logger.error("获取债权详情信息数据检查失败:订单id为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					"订单ID不能为空!");
			CommonUtil.responseJson(result, response);
			return;
		}

		if (StringUtils.isBlank(userId)) {
			logger.error("获取债权详情信息数据检查失败:订单id为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					"用户ID不能为空!");
			CommonUtil.responseJson(result, response);
			return;
		}

		try {
			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderId", orderId);

			String result = orderService.getCreditInfoList(map);

			// 处理返回结果
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("Order/getCreditInfoList 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	
	/**
	 * 查询定期3月期、6月期和发现的订单详情
	 */
	@RequestMapping(value = "/queryCusOrderList")
	public void queryCusOrderList(HttpServletRequest request, HttpServletResponse response) {

		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String logInfo = "queryCusOrderList:查询定期3月期、6月期和发现的订单详情";
		String userName = paramsMap.get("userName");// 理财人
		String phone = paramsMap.get("phone");// 手机号码
		String orderId = paramsMap.get("orderId");// 订单号
		String flag = paramsMap.get("flag");//发现
		String periods = paramsMap.get("periods");//定期3月期和6月期
		String currentPageStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);// 当前页
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);// 页大小
		Integer current = 1;
		Integer pageSize = 15;// 默认显示15条

		// 程序必须参数未传入
		if (StringUtils.isBlank(flag) && StringUtils.isBlank(periods)) {
			logger.error("查询定期3月期、6月期和发现的订单数据检查失败:参数为空");
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			return;
		}

		try {
			
			if(null==userName || "null".equalsIgnoreCase(userName)){
				userName = "";
			}
			if(null==phone || "null".equalsIgnoreCase(phone)){
				phone = "";
			}
			if(null==orderId || "null".equalsIgnoreCase(orderId)){
				orderId = "";
			}
			if(null==periods || "null".equalsIgnoreCase(periods)){
				periods = "";
			}
			if(null==flag || "null".equalsIgnoreCase(flag)){
				flag = "";
			}
			// 分页参数
			if (currentPageStr != null && !("".equals(currentPageStr))) {
				current = Integer.parseInt(currentPageStr);
			}
			if (pageSizeStr != null && !("".equals(pageSizeStr))) {
				pageSize = Integer.parseInt(pageSizeStr);
			}

			Integer rowBegin = (current-1) *pageSize +1;//开始笔数
			Integer rowEnd = current *pageSize;//结束笔数
			// 获取订单列表信息
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("flag", flag);
			map.put("periods", periods);
			map.put("rowBegin", rowBegin);
			map.put("rowEnd",rowEnd);
			map.put("userName",userName);
			map.put("phone", phone);
			map.put("orderId", orderId);
			map.put("agreementNo",paramsMap.get("agreementNo"));
			map.put("startEndDate",paramsMap.get("startEndDate"));
			map.put("endEndDate",paramsMap.get("endEndDate"));
			
			Map<String, Object> jsonMap = orderService.queryCusOrderList(logInfo,map);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
			
		} catch (Exception e) {
			logger.error("Order/queryCusOrderList 异常:" + e.getMessage());
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
}
